//
//  NameAddViewController.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/11/25.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "NameAddViewController.h"
#import "FrindManage.h"
#import "JZAddFriendViewController.h"
#import "User.h"
#import "Friend.h"
#import "TalkManage.h"
#import "ContactInfo.h"
#import <RongIMKit/RongIMKit.h>

@interface NameAddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userbnameTextfild;
@property(nonatomic,strong)User *user;
@end

@implementation NameAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取当前用户名
    AVUser *currentUser = [AVUser currentUser];
    NSString *myname = currentUser.username;
    [[FrindManage defulManager] getFriendFromUserWithHandle:^{
        
    }];
    
    //判断是否要打开User表
    if ([[TalkManage defulManage] getbackFriendMessageFromCocaDataWithUsername:myname].count > 0) {
        self.user = [[TalkManage defulManage] getbackFriendMessageFromCocaDataWithUsername:myname].lastObject;
    }else{
        self.user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[TalkManage defulManage].managedObjectContext];
    }
}

- (IBAction)addfriend:(id)sender {
        //将好友信息添加到_User表中的friend字段下
        [[FrindManage defulManager] addFriendWithUsername:self.userbnameTextfild.text Handle:^{
            //将好友信息储存在本地
            [[FrindManage defulManager] StoreFriendMessageWithUser:self.user Handle:^{
            }];
            if([self.Mydelegate respondsToSelector:@selector(buttonRelead)]){
                [self.Mydelegate buttonRelead];
            }
        }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
